﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ArtClub.DBAL;
using ArtClub.Controller;

namespace ArtClub.View
{
    public partial class BiddingPage1 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

            if (Request.QueryString["ajax"] != null && Request.QueryString["id"] != null)
            {
                AuctionCtr aCtr = new AuctionCtr();
                Auction a = aCtr.getAuction(Convert.ToInt32(Request.QueryString["id"]));
                Response.Write(a.highestBid.ToString());
                Response.End();
            }
            else if (Request.QueryString["id"] != null)
            {
                AuctionCtr aCtr = new AuctionCtr();
                Auction a = aCtr.getAuction(Convert.ToInt32(Request.QueryString["id"]));
                if (a != null)
                {
                    lblName.Text = a.Piece.name;
                    lblArtist.Text = a.Piece.artist;
                    lblType.Text = a.Piece.type;
                    lblHighestBid.Text = Convert.ToString(a.highestBid.Value);
                    lblBiddingStep.Text = Convert.ToString(a.biddingStep.Value);
                    imgProduct.ImageUrl = a.Piece.image;
                }
                endTime.InnerHtml = Convert.ToString(a.endTime.Value);
            }
            else
            {
                Response.Redirect("BiddingInfoPage.aspx");
            }
        }

        protected void btnBid_Click(object sender, EventArgs e)
        {
            try
            {
                AuctionCtr aCtr = new AuctionCtr();
                int auctionId = Convert.ToInt32(Request.QueryString["id"]);
                int memberId = AppSession.member.userId;
                //int memberId = 1;

                double bidPrice = Convert.ToDouble(lblHighestBid.Text) + Convert.ToDouble(lblBiddingStep.Text);
                aCtr.bidPiece(auctionId, bidPrice, memberId);
                Page.ClientScript.RegisterClientScriptBlock(GetType(), "b", "bootbox.alert('Your bidding is successful')", true);
            }
            catch (Exception exc)
            {
                string message = exc.Message;
                string box = "bootbox.alert('" + exc.Message + "')";
                Page.ClientScript.RegisterClientScriptBlock(GetType(), "a", box , true);
            }
            
        }
    }
}